/*
* Bank
*
* r4
*
* GNU General Public License v3.0
*/

package com.acme.bankapp.domain.bank;

import java.util.*;

import com.acme.bankapp.domain.client.*;
import com.acme.bankapp.exceptions.*;

public class Bank {
	
	private ArrayList<Client> clients = new ArrayList<>();
	private ArrayList<ClientRegistrationListener> clientRegistrationListeners = new ArrayList<>();
	
	private class PrintClientListener implements ClientRegistrationListener {
		
		@Override
		public void onClientAdded(final Client c) {
			System.out.println("Client " + c.getClientSalutation() + " added");
		}
		
	}
	
	private class EmailNotificationListener implements ClientRegistrationListener {

		@Override
		public void onClientAdded(final Client c) {
			System.out.println("Notification email for client " + c.getClientSalutation() + " to be sent");
		}

	}
	
	private class DebugListener implements ClientRegistrationListener {

		@Override
		public void onClientAdded(final Client c) {
			System.out.println(new Date() + ": " + c.getClientSalutation() + " added");
		}
		
	}
	
	public Bank() {
		this.clientRegistrationListeners.add(new PrintClientListener());
		this.clientRegistrationListeners.add(new EmailNotificationListener());
		this.clientRegistrationListeners.add(new DebugListener());
	}
	
	public Bank(ArrayList<ClientRegistrationListener> clientRegistrationListeners) {
		this.clientRegistrationListeners = clientRegistrationListeners;
	}
	
	public void addClient(final Client c) throws ClientExistsException {
		for(Client client : clients) {
			if(client.getClientSalutation().equals(c.getClientSalutation())) {
				throw new ClientExistsException(c);
			}
		}
		
		this.clients.add(c);
		
		for(ClientRegistrationListener crl : clientRegistrationListeners) {
			crl.onClientAdded(c);
		}
	}
	
	public ArrayList<Client> getClients() {
		return this.clients;
	}
	
	public void clear() {
		this.clients.clear();
	}
	
}
